

from datetime import datetime
from typing import List

import requests

from constants.recognition import RecognitionStrategy, RecognitionStrategyCN, TimeStrategy


class Recognition:

    time_strategy:List[TimeStrategy]
    type:RecognitionStrategy

    def __init__(self):
        self.time_strategy = [] 
        self.type = None  

    @staticmethod
    def verify(data=None):
        """
        验证数据
        :param data: 任意数据
        :return: 布尔值
        """
        return True
    
    def recognize(self, data=None):
        """
        识别方法
        :param data: 任意数据
        :return: 布尔值
        """
        return True

    def regression(self, data=None):
        """
        回归方法
        :param data: 任意数据
        :return: 布尔值
        """
        return True

    def reminder(self,data, extra_message_arr=[]):
        """
        发送提醒消息
        :param basic: 基础信息字典
        :param quote: 报价信息字典
        :param extra_message_arr: 额外消息列表
        """
        info=data.get("info")
        tick=data.get("tick")
        stock_instance=data.get("stock_instance")
        triggeredTypes=list(stock_instance.triggerRecord.keys())
        triggeredTypes.append(self.type)


        # 构建消息内容
        message_parts = [
            f'股票:  <font color="info">**{info["InstrumentName"]} {info["InstrumentID"]}**</font> \n',
            f'触发类型:  <font color="comment">**{RecognitionStrategyCN[self.type]}**</font> \n',
            f'触发价格:  <font color="info">**{tick["lastPrice"]}**</font> \n',
            f'涨跌幅:     <font color="warning">**{tick["chg"]}%**</font> \n',
            f'触发时间:  <font color="comment">**{tick["current_time"]}**</font>  \n',
            *extra_message_arr,
            f'所有触发类型:  <font color="info">**{",".join([RecognitionStrategyCN[type] for type in triggeredTypes])}**</font> \n'
            f'[**打开东方财富查看详情**](eastmoney://page/stockpage?code={info["UniCode"]}&market={ "1" if info["ExchangeID"]=="SH" else "0"})'
            # f'[**打开东方财富查看详情**](eastmoney://page/stockpage?code={info["UniCode"]}&market={ "1" if info["ExchangeID"]=="SH" else "0"})'
        ]
        
        message = "".join(message_parts)

        # 检查环境变量，如果不是开发环境则发送企业微信机器人消息
        import os
        if os.environ.get("MODE") != "development":
            webhook_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=61503ede-65ef-4b6f-bfb5-64f74850f468"
            payload = {
                "msgtype": "markdown",
                "markdown": {
                    "content": message
                }
            }
            try:
                response = requests.post(webhook_url, json=payload)
                return response
            except Exception as e:
                print(f"发送企业微信消息失败: {e}")
                return None
        else:
            # 开发环境不发送消息
            pass